package com.csi.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.csi.dao.DeptDao;
import com.csi.model.Dept;

@WebServlet("/deptServlet")
public class DeptServlet extends HttpServlet {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DeptDao deptDao = new DeptDao();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 获取隐藏域中存储的方法名称
		String method = req.getParameter("method");
		if("schDept".equals(method)) {
			schDept(req, resp);
		} else if("updateDeptInit".equals(method)) {
			updateDeptInit(req, resp);
		} else if("updateDept".equals(method)) {
			updateDept(req, resp);
		} else if("addDept".equals(method)) {
			addDept(req, resp);
		} else if("deleteDept".equals(method)) {
			deleteDept(req, resp);
		}

	}
	
	public void deleteDept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String dept_id = req.getParameter("dept_id");
		// 用Dept对象对数据进行封装
		deptDao.deleteDeptById(Integer.parseInt(dept_id));
		resp.sendRedirect("deptServlet?method=schDept");
//		schDept(req, resp);
	}
	
	public void addDept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String dept_name = req.getParameter("dept_name");
		String dept_remark = req.getParameter("dept_remark");
		
		// 用Dept对象对数据进行封装
		Dept dept = new Dept();
		dept.setDept_name(dept_name);
		dept.setDept_remark(dept_remark);
		deptDao.addDept(dept);
		resp.sendRedirect("deptServlet?method=schDept");
	}
	
	public void updateDeptInit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String dept_id = req.getParameter("dept_id");
		Dept dept = deptDao.schDeptById(dept_id);
		req.setAttribute("dept", dept);
		req.getRequestDispatcher("dept/showUpdateDept.jsp").forward(req, resp);
	}
	
	
	public void updateDept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String dept_id = req.getParameter("dept_id");
		String dept_name = req.getParameter("dept_name");
		String dept_remark = req.getParameter("dept_remark");
		
		// 用Dept对象对数据进行封装
		Dept dept = new Dept();
		dept.setDept_id(Integer.parseInt(dept_id));
		dept.setDept_name(dept_name);
		dept.setDept_remark(dept_remark);
		
		deptDao.updateDept(dept);
		resp.sendRedirect("deptServlet?method=schDept");
//		schDept(req, resp);
		
	}
	
	
	/**
	 * 查询用户功能
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void schDept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String dept_name = req.getParameter("dept_name");
		
		String currPage = req.getParameter("currPage");
		
		if(currPage == null) {
			currPage = "1";
		}
		
		// 当前查询总条数
		int count = deptDao.schDeptCount(dept_name);
		int pageSize = 2;
		
		// 5%2  余数为1   5/2 = 2
		// 4%2 余数为0   4/2 = 2
		
		int totalPage = count%pageSize == 0? count/pageSize : count/pageSize + 1;
		
		int cp = Integer.parseInt(currPage);
		
//		if(cp > totalPage) {
//			cp = totalPage;
//		}
		
		int start = (cp - 1)*pageSize;
		
		List<Dept> deptList = deptDao.schDeptByPage(dept_name, start, pageSize);
		
		req.setAttribute("start", start+1);
		req.setAttribute("end", start+pageSize);
		req.setAttribute("count", count);
		req.setAttribute("currPage", currPage);
		req.setAttribute("totalPage", totalPage);
		req.setAttribute("deptList", deptList);
		req.setAttribute("deptname", dept_name);
		req.getRequestDispatcher("dept/dept.jsp").forward(req, resp);
	}
}
